<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <meta charset="utf-8">
    <meta name="viewport"
          content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <meta content="yes" name="apple-mobile-web-app-capable"/>
    <meta content="black" name="apple-mobile-web-app-status-bar-style"/>
    <meta content="telephone=no" name="format-detection"/>
    <title>校淘</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../css/lib/weui.min.css">
    <link rel="stylesheet" type="text/css" href="../css/global.css">
    <link rel="stylesheet" type="text/css" href="../css/style.css">
    <script src="../js/lib/preventLocation.js"></script>
    <script type="text/javascript">
        //定义全局变量
        var i = 0;
        //金额总和
        var money = 0;
        //计算合计价格
        var cart_money = new Object();
        //全部商品ID
        var cart_id = new Object();
        //备份商品ID，用于全选后去掉全选又再次全选
        var cart_id_copy = new Object();
    </script>
    <style type="text/css">
        .aui-refresh-load {
            position: absolute;
            top: -4.2rem;
        }

        .header_box {
            height: 44px;
            position: fixed !important;
            width: 100%;
            top: 0;
            z-index: 999999;
        }

        .aui-refresh-content {
            top: 2rem;
        }
    </style>
</head>

<body>
<!-- 头部 -->
<div class="header_box" style="background:#53cac3">
    <div class="bar bar-header" style="background:#53cac3">
        <a class="back" href="../index.html" style="padding-top:5px; display:inline-block; padding-left:10px;">
            <i class="aui-iconfont aui-icon-left" style="font-weight:800; color:#fff; font-size:20px;"></i>
        </a>
        <div id="tite" class="h1 title" style=" width: 72%; margin: 0 auto; ">购物车</div>
        <a class="rig_shai" id="rem_s" href="javascript:void(0)"
           style="margin-right: 2%;position: absolute; top: 5px; right: 3%;">
            <i class="aui-iconfont aui-icon-edit" style="font-weight:800; color:#fff; font-size:20px;"></i>
            <i class="aui-iconfont js-shopcart-ok"
               style="font-weight:800; color:#fff; font-size:20px;display: none">完成</i>
        </a>
    </div>
</div>
<!-- <p id="height" style="position:absolute;top:50%;z-index:9;left:50%;font-size:18px;">高度：</p> -->
<div class="aui-refresh-content">
    <div class="aui-content">
        <div id="demo">
            <form method="post" name="cart_form" target="_self" class="swiper-container" id="cart_form" action="">
                <div class="commodity_list_box aui-card-list">
                    <div class="cart_top">
                        <span>商品清单</span>
                        <div class="clear"></div>
                    </div>
                    <!--商品列表-->
                    <div id="shoplist">
                    </div>
                    <div style="height: 50px;"></div>
                    <!-- 商品列表 end -->
                </div>
            </form>
        </div>
    </div>
</div>
<!-- 全选合计 -->
<div class="settle_box">
    <dl class="all_check select">
        <dt>
            <span id="all_pitch_on" onclick="allChoose()"></span>
            <em>全选</em>
        </dt>
    </dl>
    <dl class="total_amount">
        <dt>
            合计：
            <p id="total_price">
                ¥<b>0</b>
            </p>
        </dt>
        <dd>不含运费</dd>
    </dl>
    <input type="hidden" name="gcs" id="gcs"/>
    <a class="settle_btn" href="javascript:void(0);" id="confirm_cart">去结算</a>
    <a class="settle_btn" href="javascript:void(0);" id="confirm_cart1">删除</a>
</div>
<!-- 全选 -->


<!-- 刷新 -->

<script src="../js/lib/jquery-1.8.3.min.js"></script>
<script src="../js/lib/weui.min.js"></script>
<script src="../js/Common.js"></script>
<script src="../js/Model.js"></script>
<!--<script src="../js/views/shopcart.js"></script>-->

<script>

    var result = Model.shopcart.findAllShopcartItemByUserid({
        id: '',
        pageSize: 10,
        page: 1
    })

    for (var i = 0; i < result.goods.length; i++) {
        //全都未选中设置为
        result.goods[i].isSelect = false;
    }
    showList();

    //显示
    function showList() {
        calculate();
        isAllchoose();
        var lhtml = "";
        for (var i = 0; i < result.goods.length; i++) {
            //全都未选中设置为
            // result.goods[i].isSelect = false;
            console.log(result.goods[i].isSelect)
            lhtml += '<div class="commodity_box">'
            lhtml += '<div class="commodity_list">'
            lhtml += '<div class="tite_tim select">'
            lhtml += '<em aem="1" cart_id="84"></em>'
            lhtml += '<span>' + result.users[i].nickname + '</span>'
            lhtml += ' <div class="clear"></div>'
            lhtml += '</div>'
            lhtml += '<ul class="commodity_list_term">'
            lhtml += '<li class="select">'
            if (result.goods[i].isSelect) {
                lhtml += ' <em onclick="choose(' + result.goods[i].id + ')" aem="0" cart_id="84" class="pitch_on"></em>'
            } else {
                lhtml += ' <em aem="0" onclick="choose(' + result.goods[i].id + ')" cart_id="84"></em>'
            }
            lhtml += ' <img src="' + ((result.goods[i].goodsImg.length > 0) ? result.goods[i].goodsImg[0].image : '') + '" />'
            lhtml += ' <div class="div_center">'
            lhtml += ' <h4>' + result.goods[i].content + '</h4>'
            lhtml += '<p class="now_value">'
            lhtml += '<i>￥</i>'
            lhtml += '<b class="qu_su">' + result.goods[i].price + '</b>'
            lhtml += '</p>'
            lhtml += '</div>'
            lhtml += ' <div class="div_right">'
            lhtml += '<i onclick="reducew(' + result.goods[i].id + ')">-</i>'
            lhtml += '<span class="zi">' + result.shopcarts[i].c.num + '</span>'
            lhtml += '<input type="hidden" value="84">'
            lhtml += '<i onclick="plusw(' + result.goods[i].id + ')">+</i>'
            lhtml += '</div>'
            lhtml += '</li>'
            lhtml += '</ul>'
            lhtml += ' </div>'
            lhtml += '</div>'
        }
        $("#shoplist").empty().append(lhtml);

    }

    //计算价格
    function calculate() {
        var count = 0;
        for (var i = 0; i < result.goods.length; i++) {
            if (result.goods[i].isSelect) {
                var shopP = result.shopcarts[i].c.num * result.goods[i].price;
                count += shopP;
            }
        }
        $("#total_price b").text(count)

    }

    //减
    function reducew(id) {
        for (var i = 0; i < result.goods.length; i++) {
            if (id == result.goods[i].id && result.shopcarts[i].c.num > 1) {
                result.shopcarts[i].c.num--;
            }
        }
        showList();
    }

    //加
    function plusw(id) {
        for (var i = 0; i < result.goods.length; i++) {
            if (id == result.goods[i].id && result.shopcarts[i].c.num >= 1) {
                result.shopcarts[i].c.num++;
            }
            //&& result.shopcarts[i].c.num<result.goods[i].stock
        }
        showList();
    }

    //单选
    function choose(id) {
        for (var i = 0; i < result.goods.length; i++) {
            if (id == result.goods[i].id) {
                result.goods[i].isSelect = !result.goods[i].isSelect;
            }
        }
        isAllchoose();
        showList();
    }

    //判断是否全选
    function isAllchoose() {
        var isAllSec = true;
        for (var i = 0; i < result.goods.length; i++) {
            if (!result.goods[i].isSelect) {
                isAllSec = false;
            }
        }
        if (isAllSec) {
            $("#all_pitch_on").addClass("pitch_on")
        } else {
            $("#all_pitch_on").removeClass("pitch_on")
        }
        return isAllSec;
    }


    //全选
    function allChoose() {

        if (isAllchoose()) {
            $("#all_pitch_on").removeClass("pitch_on")
            for (var i = 0; i < result.goods.length; i++) {
                result.goods[i].isSelect = false;
            }
        } else {
            $("#all_pitch_on").addClass("pitch_on")
            for (var i = 0; i < result.goods.length; i++) {
                result.goods[i].isSelect = true;
            }
        }
        showList();
    }


    //点击删除购物车商品
    $('.aui-icon-edit').click(function () {
        $('.aui-icon-edit').hide();
        $('.js-shopcart-ok').show();
        $('#confirm_cart').hide();
        $('#confirm_cart1').show();
    })

    //删除购物车
    $('.js-shopcart-ok').click(function () {
        $('.aui-icon-edit').show();
        $('.js-shopcart-ok').hide();
        $('#confirm_cart').show();
        $('#confirm_cart1').hide();


    })


    //删除商品
    $(document).on('click', '#confirm_cart1', function () {
        for (var i = 0; i < result.goods.length; i++) {
            //全都未选中设置为
            if (result.goods[i].isSelect) {
                var id = result.goods[i].id;
                var result2 = Model.shopcart.deleteCartGood({
                    id: id
                })
                if (result2.code == 1) {
                    weui.toast('删除成功', {
                        duration: 1500,
                        className: 'custom-classname',
                        callback: function () {
                            location.reload();
                        }
                    });
                }
            }
        }
        showList();
    })


    $('#confirm_cart').click(function () {
        var goodsids = '';
        var counts = '';
        for (var i = 0; i < result.goods.length; i++) {
            if (result.goods[i].isSelect) {
                goodsids = goodsids + result.goods[i].id + ',';
                counts = counts + result.shopcarts[i].c.num + ',';

            }
        }
        var total_price = $('#total_price b').text();
        var result1 = Model.shopcart.addNewOrderByShopCart({
            userid: '',
            goodsids: goodsids,
            counts: counts
        })
        //获得订单号
        var orderid = result1.data[0].id;
        var loading = weui.loading('loading', {
            className: 'custom-classname'
        });

        if (result.code = 1) {
            loading.hide();
            weui.dialog({
                title: '校淘支付',
                content: '￥' + total_price + '',
                className: 'custom-classname',
                buttons: [{
                    label: '取消',
                    type: 'default',
                    onClick: function () {
                    }
                }, {
                    label: '支付',
                    type: 'primary',
                    onClick: function () {
                        var result2 = Model.order.orderDetail({
                            state: 1,
                            id: orderid
                        });
                        if (result2.code = 1) {
                            weui.toast('支付成功', {
                                duration: 1500,
                                className: 'custom-classname',
                                callback: function () {

                                    location.href = 'or_buy_nopay.html';
                                }
                            });
                        }

                    }
                }]
            });
        }
        console.log(goodsids.slice(0, goodsids.length - 1))
        console.log(counts.slice(0, counts.length - 1))

        // createOrder();
    });

    function createOrder(result) {

    }

</script>
</body>

</html>